Math 425 Computation Linear Algebra

HW1, Part A

Brent A. Thorne

brentathorne@gmail.com

*See Appendix for hand calcluations
In [1]:
# environment setup
import numpy as np # Let's use numpy for this
from sympy.matrices import Matrix # Include this in case we want some pretty matrices
from sympy.solvers.solveset import linsolve
from sympy import * # import the entire namespace of sympy at root, NOT the best of practices
from math import e, pi
init_printing()  # initialize pretty printing

1. Solve the system

$ u + v + w = −2$

$3u + 3v − w = 6$

$ u − v + w = −1$

In [2]:
# use linalg.solve to solve system
A = np.array([[1,1,1],[3,3,-1],[1,-1,1]])
B = np.array([-2,6,-1])
print('Solution: [u,v,w] =', np.linalg.solve(A,B)) 
Solution: [u,v,w] = [ 1.5 -0.5 -3. ]
In [3]:
# let's make this prettier with sympy

u,v,w = symbols('u v w')

# quick and clean
r=linsolve([u+v+w+2, 3*u+3*v-w-6, u-v+w+1], (u,v,w)) 

# more verbose
A = Matrix([[1,1,1],[3,3,-1],[1,-1,1]])
B = Matrix([-2,6,-1])

rr= linsolve((A,B), (u,v,w))
rr, r #show both results are the same, not really any prettier though
Out[3]:
$\displaystyle \left( \left\{\left( \frac{3}{2}, \ - \frac{1}{2}, \ -3\right)\right\}, \ \left\{\left( \frac{3}{2}, \ - \frac{1}{2}, \ -3\right)\right\}\right)$

2. Choose h and k such that the system below has

(a) no solution,

(b) a unique solution, and

(c) many solutions.

Give separate answers for each part.

$x_1 + 3x_2 = 2$

$3x_1 + hx_2 = k$

In [4]:
# (a) no solution (parallel)
h=3*3
k=0
print('h = ', h, ', k = ', k) 

A = np.array([[1,3], [3,h]])
B = np.array([2,k])

try:
    np.linalg.solve(A,B) # a Singular matrix is expected if no solution
except:
    print('Singular matrix, no solution in this case, but not a suffienct test.') 
h =  9 , k =  0
Singular matrix, no solution in this case, but not a suffienct test.
In [5]:
# (a) no solution (parallel) continuned...    
# Let's try plotting the equations now
import matplotlib.pyplot as plt
import numpy as np

x1 = np.linspace(-5,5,100)
x2 = (2 - x1)/3
xx2 = (0 - 3*x1)/9
plt.plot(x1, x2, '-r', label='$x_1+3x_2=2$')
plt.plot(x1, xx2, '-g', label='$3x_1+9x_2=0$')

plt.title('System w/o Soluton')
plt.xlabel('x', color='#1C2833')
plt.ylabel('y', color='#1C2833')
plt.legend(loc='upper left')
plt.grid()
plt.show()   
In [6]:
# (b) a unique solution (beams are crossed)
h=3
k=0
print('h = ', h, ', k = ', k) 

A = np.array([[1,3], [3,h]])
B = np.array([2,k])

try:
    print('Solution', np.linalg.solve(A,B))
except:
    print('Singular matrix, no solution or many solutions.')
h =  3 , k =  0
Solution [-1.  1.]
In [7]:
# (b) a unique solution (beams are crossed) continuned...    
# Let's try plotting the equations now
import matplotlib.pyplot as plt
import numpy as np
h=3
k=0

x1 = np.linspace(-5,5,100)
x2 = (2 - x1)/3
xx2 = (k - 3*x1)/h
plt.plot(x1, x2, '-r', label='$x_1+3x_2=2$')
plt.plot(x1, xx2, '-g', label='$3x_1+3x_2=0$')

plt.title('System w/Unique Soluton')
plt.xlabel('x', color='#1C2833')
plt.ylabel('y', color='#1C2833')
plt.legend(loc='upper left')
plt.grid()
plt.show()

print('BTW: How do you catch an unique animal?')
print('     Unique up on it, of course.')
BTW: How do you catch an unique animal?
     Unique up on it, of course.
In [8]:
# (c) many solutions (coeffs are of equal ratios, meaning lines are on top of each other)
h=3*3
k=2*3
print('h = ', h, ', k = ', k) 

A = np.array([[1,3], [3,h]])
B = np.array([2,k])
try:
    print('Solution', np.linalg.solve(A,B))
except:
    print('Singular matrix, many solutions in this case.\nAgain not sufficient test but demonstrated graphically below.')  
h =  9 , k =  6
Singular matrix, many solutions in this case.
Again not sufficient test but demonstrated graphically below.
In [9]:
# (c) many solutions continuned...    
# Let's try plotting the equations now
import matplotlib.pyplot as plt
import numpy as np
h=9
k=6

x1 = np.linspace(-5,5,100)
x2 = (2 - x1)/3
xx2 = (k - 3*x1)/h
plt.plot(x1, x2, '-r', label='$x_1+3x_2=2$')
plt.plot(x1, xx2, '--g', label='$3x_1+9x_2=6$')

plt.title('System w/Many Solutons')
plt.xlabel('x', color='#1C2833')
plt.ylabel('y', color='#1C2833')
plt.legend(loc='upper left')
plt.grid()
plt.show()

3. Consider the system of equations below.

$4x_1 + x_2 + 3x_3 = 9$

$x_1 − 7x_2 − 2x_3 = 12$

$8x_1 + 6x_2 − 5x_3 = 15$

In [10]:
# first solve it
A = Matrix([[4,1,3],[1,-7,-2],[8,6,-5]])
B = Matrix([9,12,15])

x1,x2,x3= symbols('x_1 x_2 x_3')
xx= linsolve((A,B), (x1,x2,x3))
xx
Out[10]:
$\displaystyle \left\{\left( \frac{328}{121}, \ - \frac{14}{11}, \ - \frac{23}{121}\right)\right\}$

(a) Column-space view:

Find the vectors $v_1 , v_2 , v_3$ and write the system as a vector equation

$x_1 v_1 + x_2 v_2 + x_3 v_3 = \begin{bmatrix} 9 \\ 12 \\ 15 \\ \end{bmatrix} $

In [11]:
# think about how best to show this using python
# of course we can just do this by hand, which we will 

# use sympy to make some pretty stuff
v1=Matrix([[4],[1],[8]])
v2=Matrix([[1],[-7],[6]])
v3=Matrix([[3],[-2],[-5]])

B = Matrix([[9],[12],[15]])
x1,x2,x3= symbols('x_1 x_2 x_3')
A = x1*v1 + x2*v2 + x3*v3
(x1,x2,x3)=xx.args[0] # use result from above
BB = x1*v1 + x2*v2 + x3*v3

# show our varibles to demonstrate that A and B bits are equalivent to the system
[v1, v2, v3, A, B, BB, (x1,x2,x3) ]
Out[11]:
$\displaystyle \left[ \left[\begin{matrix}4\\1\\8\end{matrix}\right], \ \left[\begin{matrix}1\\-7\\6\end{matrix}\right], \ \left[\begin{matrix}3\\-2\\-5\end{matrix}\right], \ \left[\begin{matrix}4 x_{1} + x_{2} + 3 x_{3}\\x_{1} - 7 x_{2} - 2 x_{3}\\8 x_{1} + 6 x_{2} - 5 x_{3}\end{matrix}\right], \ \left[\begin{matrix}9\\12\\15\end{matrix}\right], \ \left[\begin{matrix}9\\12\\15\end{matrix}\right], \ \left( \frac{328}{121}, \ - \frac{14}{11}, \ - \frac{23}{121}\right)\right]$

(b) Row-space view:

Find the vectors $w_1 , w_2 , w_3$ and $x$ such that the system is equivalent to

$w_1 · x = 9$

$w_2 · x = 12$

$w_3 · x = 15$

In [12]:
# same idea as above but now with row vectors

# use sympy to make some pretty stuff
w1 = Matrix([[4,1,3]])
w2 = Matrix([[1,-7,-2]])
w3 = Matrix([[8,6,-5]])
b1 = 9
b2 = 12
b3 = 15

x1,x2,x3 = symbols('x_1 x_2 x_3')
x = Matrix([[x1], [x2], [x3]])

a1 = w1*x
a2 = w2*x
a3 = w3*x

# show our varibles to demonstrate that our system is equalivent
[w1,a1,b1], [w2,a2,b2], [w3,a3,b3], x, xx
Out[12]:
$\displaystyle \left( \left[ \left[\begin{matrix}4 & 1 & 3\end{matrix}\right], \ \left[\begin{matrix}4 x_{1} + x_{2} + 3 x_{3}\end{matrix}\right], \ 9\right], \ \left[ \left[\begin{matrix}1 & -7 & -2\end{matrix}\right], \ \left[\begin{matrix}x_{1} - 7 x_{2} - 2 x_{3}\end{matrix}\right], \ 12\right], \ \left[ \left[\begin{matrix}8 & 6 & -5\end{matrix}\right], \ \left[\begin{matrix}8 x_{1} + 6 x_{2} - 5 x_{3}\end{matrix}\right], \ 15\right], \ \left[\begin{matrix}x_{1}\\x_{2}\\x_{3}\end{matrix}\right], \ \left\{\left( \frac{328}{121}, \ - \frac{14}{11}, \ - \frac{23}{121}\right)\right\}\right)$

4. Determine if b is a linear combination of the vectors formed from the columns of the matrix A.

$A= \begin{bmatrix} 1 & -2 & -6 \\ 0 & 3 & 6 \\ 1 &-2 & 5 \\ \end{bmatrix}, $ $B= \begin{bmatrix} 11 \\ -5 \\ 9 \\ \end{bmatrix}$

In [13]:
# by hand we would row reduce the augmented matrix and check if it has a solution
# here we simply show that a solution exits
A = np.array([[1,-2,-6],[0,3,6],[1,-2,5]])
B = np.array([11,-5,9])
np.linalg.solve(A,B)  # it exists therefore it is a linear combination, isn't Linear Algebra fun with Python?!!
Out[13]:
array([ 7.3030303 , -1.3030303 , -0.18181818])
In [14]:
# 4. continued (use symp rref as example)
A = Matrix([[1,-2,-6],[0,3,6],[1,-2,5]])
B = Matrix([11,-5,9])
M=A.col_insert(4,B)

x1,x2,x3 = symbols('x_1 x_2 x_3')
Mrref = M.rref(pivots=False)
R = Mrref.col(-1)
print('Result as float:\n', np.array(Matrix.tolist(R)).astype(np.float64)) # show float numbers are same as above
Mrref.col(-1), linsolve((A,B), (x1,x2,x3)) # show rref and linsolve provide same result
Result as float:
 [[ 7.3030303 ]
 [-1.3030303 ]
 [-0.18181818]]
Out[14]:
$\displaystyle \left( \left[\begin{matrix}\frac{241}{33}\\- \frac{43}{33}\\- \frac{2}{11}\end{matrix}\right], \ \left\{\left( \frac{241}{33}, \ - \frac{43}{33}, \ - \frac{2}{11}\right)\right\}\right)$

5. Let $f (z) = az + b$ where $z ∈ \mathbb{C}$.

Find $a$ and $b$ if $f(z)$ translates $z$ one unit up and one unit to the right,

rotates the result by $\frac \pi 2$ clockwise and

scales the resulting complex number by 2.

In [15]:
# Don't we wish we did this HW problem before the quiz?  Anyway, this is for the joy of it all.
# The operations again are translate, rotate and scale.
def f(z): return ( (z + (1+1j) * e**(-1j*pi/2)) * 2) 
z=0
t= f(z)

a = -2j
b = 2-2j
tt= a * z + b
(t.real, t.imag), (tt.real, tt.imag) # show both hand calc and script are equal
Out[15]:
$\displaystyle \left( \left( 2.0, \ -2.0\right), \ \left( 2.0, \ -2.0\right)\right)$

Appendix 1. Hand Calculations

Time to break out the Cognac and compute like it's 1855.

330px-Carl_Friedrich_Gauss_1840_by_Jensen.jpg

math425_hw1_handcalc.png-1.png

math425_hw1_handcalc.png-2.png

math425_hw1_handcalc.png-3.png

math425_hw1_handcalc.png-4.png

math425_hw1_handcalc.png-5.png

*Note: $(x1,x2,x3) = (\frac{328}{121}, -\frac{14}{11}, -\frac{23}{121})$ and $x=\begin{bmatrix}\frac{328}{121}\\ -\frac{14}{11}\\ -\frac{23}{121}\end{bmatrix}$ for P3 above.

math425_hw1_handcalc.png-6.png

math425_hw1_handcalc.png-7.png